package com.example.demo.service;


import com.example.demo.entity.CompanyInfo;
import com.example.demo.entity.CompanyUser;
import com.example.demo.entity.UserInfo;
import com.example.demo.mapper.CompanyInfoMapper;
import com.example.demo.mapper.CompanyUserMapper;
import com.example.demo.mapper.UserInfoMapper;
import groovy.util.logging.Slf4j;
import io.shardingjdbc.core.api.HintManager;
import io.shardingjdbc.core.hint.HintManagerHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class DemoService {

    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    CompanyInfoMapper companyInfoMapper;
    @Resource
    CompanyUserMapper companyUserMapper;

    public static Long userId = 150L;

    public void insertComapny() {

        for (int i = 1; i <= 10; i++) {
            CompanyInfo userInfo = new CompanyInfo();
            userInfo.setCompanyName("名字"+i);
            userInfo.setCompanyId((long) i);
            if(i==3){
                HintManagerHolder.clear();
                HintManager hintManager = HintManager.getInstance();
                hintManager.addDatabaseShardingValue("company_info", "company_id", 3L);
                hintManager.addTableShardingValue("company_info", "company_id", 3L);
            }
            companyInfoMapper.insert(userInfo);

        }
    }


    public void insertCompanyUser(){
        for (int i = 1; i <= 10; i++) {
            CompanyUser obj = new CompanyUser();
            obj.setCompanyId(i);
            obj.setId((long) i);
            obj.setUserId(i);
            if(i==3){
                HintManagerHolder.clear();
                HintManager hintManager = HintManager.getInstance();
                hintManager.addDatabaseShardingValue("company_info", "company_id", 3L);
                hintManager.addTableShardingValue("company_info", "company_id", 3L);
            }
            companyUserMapper.insert(obj);

        }

    }


    public void insertUser(){
        for (int i = 1; i <= 10; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setAccount("Account" + i);
            userInfo.setPassword("pass" + i);
            userInfo.setUserName("name" + i);
            userInfo.setUserId((long)i);
            if(i==3){
                HintManagerHolder.clear();
                HintManager hintManager = HintManager.getInstance();
                hintManager.addDatabaseShardingValue("user_info", "user_id", 3L);
                hintManager.addTableShardingValue("user_info", "user_id", 3L);
            }
            userInfoMapper.insert(userInfo);

        }

    }


    public UserInfo selectByPrimaryKey(Long userId){
        return userInfoMapper.selectByPrimaryKey(userId);
    }

    public List<UserInfo> findAll() {

        return userInfoMapper.findAll();
    }

    public List<UserInfo> findAll2() {
        return userInfoMapper.findAll2();
    }

    public List<UserInfo> findAll3() {
        return userInfoMapper.findAll3();
    }

    public List<HashMap<String,Object>> findAll4() {
        return userInfoMapper.findAll4();
    }
}
